package com.kuzlak.cms.module.shop.controller;

import cn.hutool.core.bean.BeanUtil;
import com.kuzlak.cms.bean.MyPage;
import com.kuzlak.cms.bean.Result;
import com.kuzlak.cms.module.shop.bean.ShopCategoryVO;
import com.kuzlak.cms.module.shop.entity.ShopCategory;
import com.kuzlak.cms.module.shop.service.ShopCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "店铺分类管理")
@RestController
@RequestMapping("shop/category")
public class ShopCategoryController {
    @Autowired
    private ShopCategoryService shopCategoryService;

    @Operation(summary = "获取商店分类列表", description = "分页查询商店分类")
    @GetMapping("list")
    public Result list(@RequestParam(name = "page", defaultValue = "0") int page, @RequestParam(name = "pagesize", defaultValue = "10") int pagesize) {
        MyPage<ShopCategory> list = shopCategoryService.findByPage(page, pagesize);
        return Result.ok(list);
    }

    @Operation(summary = "获取商店分类列表", description = "分页查询商店分类")
    @GetMapping("miniList")
    public Result miniList() {
        List<ShopCategory> list = shopCategoryService.list();
        return Result.ok(list);
    }
    @Operation(summary = "添加或更新", description = "创建并保存新的商店分类或根据ID更新商店分类")
    @PostMapping("edit")
    public Result edit(@RequestBody ShopCategoryVO param) {
        ShopCategory  data= BeanUtil.toBean(param, ShopCategory.class);
        if(data.getId()!=null){
            data.setId(0L);
        }
        boolean b = false;
        if (data.getId()>0){
            b = shopCategoryService.updateById(data);
        }else{
             b = shopCategoryService.save(data);

        }
        return b?Result.ok():Result.failure();

    }

    @Operation(summary = "删除", description = "根据ID删除商店分类")
    @GetMapping("delete")
    public Result delete(@RequestParam("id") Integer id) {
        boolean b = shopCategoryService.removeById(id);
        return b?Result.ok():Result.failure();
    }
}
